package com.ff.discounts.component.addressdetail

import androidx.lifecycle.viewModelScope
import com.ff.discounts.component.address.AddressData
import com.ff.discounts.component.address.AreaData
import com.ff.discounts.component.address.NewAreaData
import com.ff.discounts.entity.response.onSuccess
import com.ff.discounts.library.log.HiLog
import com.ff.discounts.model.BaseViewModel
import com.ff.discounts.repository.DefaultNetworkRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch

/**
 *收货地址详情界面
 * @author: fufang
 * @date: 2023/11/1
 */
class AddressDetailViewModel(private val id:String?): BaseViewModel() {
    private val _data = MutableSharedFlow<AddressData>()
    val data: Flow<AddressData> =_data

    //获取省市区三级列表
    private val _areaData = MutableSharedFlow<NewAreaData>()
     val areaData: Flow<NewAreaData> = _areaData

    val params:AddressData?=null

    /**
    * 如果地址id不为空
    */
    fun loadData(){
        getArea()
        if(id == null){
            return
        }
    }
    /**
    * 保存地址
    */

    /**
    * 获取省市区三级列表
    */
    private fun getArea(){
        viewModelScope.launch(coroutineExceptionHandler) {
            DefaultNetworkRepository.getArea().onSuccess(viewModel){
                if (it != null) {
                    _areaData.emit(it)
                }
            }
        }
    }

}